package designs.models.strategy.example.one;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Strategy2 implements EncryptStrategy {
	String password;
	public Strategy2() {
		this.password = "I love this game";
	}
	
	public Strategy2(String password) {
		super();
		this.password = password;
	}

	@Override
	public void encryptFile(File file) {
		try {
			byte[] bs = password.getBytes();
			FileInputStream in = new FileInputStream(file);
			long length = file.length();
			byte[] c = new byte[(int)length];
			int m = in.read(c);
			for (int i = 0; i < m; i++) {
				int n = c[i]^bs[i%bs.length];
				c[i] = (byte)n;
			}
			in.close();
			FileOutputStream out = new FileOutputStream(file);
			out.write(c, 0, m);
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	public String decryptFile(File file) {
		try {
			byte[] bs = password.getBytes();
			int length = (int) file.length();
			FileInputStream in = new FileInputStream(file);
			byte[] c = new byte[length];
			int m = in.read(c);
			for (int i = 0; i < m; i++) {
				int n = c[i]^bs[i%bs.length];
				c[i] = (byte) n;
			}
			in.close();
			return new String(c,0,m);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
